﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using QuanLyThuVien.DTO;

namespace QuanLyThuVien.DAO
{
    class PhieuTraDAO
    {
        //ha phuong
        public static void LapPhieuTra(PhieuTraDTO x)
        {
            NorthwindDataContext QLTV = new NorthwindDataContext();
            PhieuTra p = new PhieuTra();
            p.NgayTra = x.NgayTra1;
            p.TienPhatKyNay = x.TienPhatKyNay1;
            p.TienNo = x.TienNo1;
            p.MaPhieuMuon = x.MaPhieuMuon1;
            QLTV.PhieuTras.InsertOnSubmit(p);
            QLTV.SubmitChanges();
        }

        //truyen
        public static DataTable SachDaTra(DateTime ngayTra)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var sachTra = from pt in db.PhieuTras
                          join ctpt in db.ChiTietPhieuTras on pt.MaPhieuTra equals ctpt.MaPhieuTra
                          join ctpm in db.ChiTietPhieuMuons on ctpt.MaChiTietPhieuMuon equals ctpm.MaChiTietPhieuMuon
                          join pm in db.PhieuMuons on ctpm.MaPhieuMuon equals pm.MaPhieuMuon
                          join s in db.Saches on ctpm.MaSach equals s.MaSach
                          where pt.NgayTra.Date == ngayTra.Date && (pt.NgayTra - pm.NgayMuon).TotalDays > 4
                        
                          select new
                          {
                              pm.NgayMuon,
                              s.TenSach,
                              ngaytratre = (pt.NgayTra - pm.NgayMuon).TotalDays

                          };

            DataTable dt = new DataTable();
            dt.Columns.Add("NgayMuon", typeof(DateTime));
            dt.Columns.Add("TenSach", typeof(string));
            dt.Columns.Add("ngaytratre", typeof(int));
            foreach (var gtri in sachTra)
            {
                dt.Rows.Add(gtri.NgayMuon, gtri.TenSach, gtri.ngaytratre);
            }

            return dt;
        }
    }
}
